//
//  ContentView.swift
//  focusOn
//
//  Created by 恒 on 2025/7/3.
//

import SwiftUI
import Combine
import UIKit

// 圆形进度指示器
struct CircularProgressView: View {
    var progress: Double
    var color: Color

    var body: some View {
        ZStack {
            // 背景圆环
            Circle()
                .stroke(Color.gray.opacity(0.2), lineWidth: 12)
            
            // 进度圆环
            Circle()
                .trim(from: 0, to: CGFloat(progress))
                .stroke(color, style: StrokeStyle(lineWidth: 12, lineCap: .round))
                .rotationEffect(.degrees(-90))
                .animation(.linear(duration: 1), value: progress)
        }

    }
}

// 自定义按钮样式
struct PrimaryButtonStyle: ButtonStyle {
    var backgroundColor: Color
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .font(.title)
            .fontWeight(.semibold)
            .padding()
            .frame(maxWidth: .infinity)
            .background(backgroundColor)
            .foregroundColor(.white)
            .cornerRadius(16)
            .shadow(color: backgroundColor.opacity(0.3), radius: 10, x: 0, y: 5)
            .scaleEffect(configuration.isPressed ? 0.95 : 1.0)
            .animation(.easeInOut, value: configuration.isPressed)
    }
}

struct ContentView: View {
    @StateObject var viewModel = TimerViewModel()
    var body: some View {
        ZStack {
            // 渐变背景
            RoundedRectangle(cornerRadius: 20)
                .fill(LinearGradient(gradient: Gradient(colors: [Color.blue.opacity(0.1), Color.purple.opacity(0.1)]), startPoint: .topLeading, endPoint: .bottomTrailing))
                .edgesIgnoringSafeArea(.all)
            
            // 左上角积分显示
            VStack {
                HStack {
                    HStack(alignment: .top, spacing: 16) {
                        HStack(spacing: 5,) {
                            Image(systemName: "star.fill")
                                .foregroundColor(.yellow)
                            Text("\(viewModel.score)")
                        }
                            .font(.headline)
                            .fontWeight(.bold)
                        HStack(spacing: 4) {
                            Image(systemName: "clock.fill")
                                .foregroundColor(.primary)
                            Text("\(viewModel.totalMinutes)分")
                        }
                            .font(.headline)
                            .fontWeight(.bold)
                    }
                    .padding()
                    Spacer()
                    Button(action: {
                        viewModel.showingMusicSettings = true
                    }) {
                        Image(systemName: "music.note")
                            .font(.headline)
                            .frame(width: 50, height: 50)
                            .background(Color.blue.opacity(0.1))
                            .cornerRadius(25)
                    }

                    Button(action: {
                        viewModel.showingModeSelection = true
                    }) {
                        Image(systemName: "gearshape")
                            .font(.headline)
                            .frame(width: 50, height: 50)
                            .background(Color.blue.opacity(0.1))
                            .cornerRadius(25)
                    }
                }
                Spacer()
            }
            .padding()
            
            VStack(spacing: 30) {

        // 警告提示
                if viewModel.isTimerRunning {
//                    Text("如果停止小猫将会挂掉!")
//                        .font(.title2)
//                        .fontWeight(.semibold)
//                        .foregroundColor(.red)
//                        .padding()
//                        .background(Color.red.opacity(0.1))
//                        .cornerRadius(12)
                }

            // 番茄钟阶段显示
            if viewModel.selectedMode == .pomodoro {
                Text(viewModel.isWorkPhase ? "工作阶段" : "休息阶段")
                    .font(.title)
                    .fontWeight(.semibold)
                    .padding(.horizontal, 20)
                    .padding(.vertical, 8)
                    .background(viewModel.isWorkPhase ? Color.red.opacity(0.1) : Color.green.opacity(0.1))
                    .foregroundColor(viewModel.isWorkPhase ? .red : .green)
                    .cornerRadius(20)
            }

            // 计时器卡片
            ZStack {
                Text(viewModel.timeString(time: viewModel.timeRemaining))
                    .font(.system(size: 64, weight: .bold))
                    .foregroundColor(.primary)
                    .minimumScaleFactor(0.5)
                    .padding()
//                    .animation(.easeOut, value: viewModel.timeRemaining)
            }
            .frame(height: 220)
            .scaleEffect(viewModel.isTimerRunning ? 1.05 : 1.0)
            .animation(.easeInOut(duration: 0.3), value: viewModel.isTimerRunning)
            

            if viewModel.isCanceling {
                Text("20秒内停止不会有惩罚，剩余 \(viewModel.cancelCountdown) 秒")
                    .font(.title2)
                    .foregroundColor(.orange)
            }

            Button(action: { viewModel.toggleTimer() }) {
                Text(viewModel.isTimerRunning ? "停止" : "开始")
            }
            .buttonStyle(PrimaryButtonStyle(backgroundColor: viewModel.isTimerRunning ? .red : .green))
            .padding(.horizontal)
        }
        }
            .padding()
            .sheet(isPresented: $viewModel.showingModeSelection) {
                ZStack {
                    RoundedRectangle(cornerRadius: 30)
                        .fill(Color.white)
                        .shadow(color: Color.black.opacity(0.1), radius: 20)
                    
                    VStack(alignment: .leading, spacing: 15) {
                        Picker("模式", selection: $viewModel.selectedMode) {
                            Text("自定义分钟").tag(TimerMode.custom)
                            Text("番茄钟").tag(TimerMode.pomodoro)
                        }
                        .pickerStyle(SegmentedPickerStyle())
                        .padding(.horizontal)
                        .scaleEffect(1.05)
                        
                        if viewModel.selectedMode == .custom {
                            VStack(spacing: 10) {
                                Text("设置时间")
                                    .font(.headline)
                                    .foregroundColor(.secondary)
                                
                                HStack(spacing: 10) {
                                    Stepper(value: $viewModel.defaultMinutes, in: 5...60, step: 1) {
                                        Text("\(viewModel.defaultMinutes) 分钟")
                                            .font(.title)
                                            .fontWeight(.semibold)
                                    }
                                    .padding(.horizontal)
                                    
                                   
                                }
                                
                            
                                
                                ScrollView(.vertical, showsIndicators: false) {
                                    VStack(spacing: 10) {
                                        if viewModel.savedModes.isEmpty {
                                            Text("暂无保存的模式")
                                                .font(.body)
                                                .foregroundColor(.secondary)
                                                .padding()
                                        } else {
                                            ForEach(viewModel.savedModes, id: \.self) {
                                                mode in
                                                Button(action: {
                                                    viewModel.defaultMinutes = mode
                                                }) {
                                                    Text("\(mode) 分钟")
                                                        .font(.body)
                                                        .fontWeight(.medium)
                                                }
                                                .buttonStyle(PrimaryButtonStyle(backgroundColor: viewModel.defaultMinutes == mode ? .blue : .purple))
                                                .frame(maxWidth: .infinity)
                                            }
                                        }
                                    }
                                    .padding(.horizontal)
                                }
                                .frame(height: 450) // 调整滚动视图高度

                                // 右下方删除按钮 - 始终显示
                                HStack {
                                    Spacer()
                                    Button(action: {
                                        viewModel.deleteMode(viewModel.defaultMinutes)
                                    }) {
                                        Text("删除")
                                            .font(.caption)
                                            .fontWeight(.medium)
                                    }
                                    .buttonStyle(PrimaryButtonStyle(backgroundColor: .red))
                                    .padding(.horizontal, 5)
                                    
                                    Button(action: {
                                        viewModel.saveCustomMode()
                                    }) {
                                        Text("保存")
                                            .font(.caption)
                                            .fontWeight(.medium)
                                    }
                                    .buttonStyle(PrimaryButtonStyle(backgroundColor: .blue))
                                    .padding(.horizontal, 2)
                                }
                            }
                        } else {
                            VStack(spacing: 10) {
                                Text("番茄钟设置")
                                    .font(.headline)
                                    .foregroundColor(.secondary)
                                Text("25分钟工作 + 5分钟休息")
                                    .font(.title)
                                    .fontWeight(.semibold)
                            }
                        }
                        
                        Spacer()
                    }
                    .frame(maxHeight: .infinity)
                    .padding(30)
                }
                .padding()
            }
            .sheet(isPresented: $viewModel.showingMusicSettings) {
                VStack(spacing: 30) {
                    Text("音乐设置")
                        .font(.largeTitle)
                        .fontWeight(.bold)
                    
                    Toggle("启动时播放音乐", isOn: $viewModel.playMusicOnLaunch)
                        .font(.title2)
                        .padding()
                        .background(Color.blue.opacity(0.1))
                        .cornerRadius(12)
                    
                    Text("选择背景音乐")
                        .font(.title2)
                        .fontWeight(.semibold)
                    
                    // 提取音乐选择器为单独的视图
                    MusicPickerView(viewModel: viewModel)
                    .frame(height: 600)
                    .background(Color.blue.opacity(0.05))
                    .cornerRadius(12)
                    
                    Spacer()
                }
                .padding(30)
            }
    }


}

#Preview { ContentView() }
